package com.hzqy.service.content;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.content.ContentDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.CmCatalogVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.UserHistoryVo;

@Service
public class ContentServiceBean  extends GenericServiceBean<CmContentVo,ContentDao, Integer>{
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final long serialVersionUID = 1L;
	@Resource
	private ContentDao contentDao;
	@Override
	public ContentDao getEntityDao() {
		return contentDao;
	}
	public Map<String, Object> updateContentCodeByName(CmContentVo cmCententVo) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(cmCententVo==null){
			LOG.debug("内容不存在");
			return resultMap;
		}
		if(contentDao==null){
			LOG.debug("dao fail");
		}
		Integer falg=contentDao.updateContentCodeByName(cmCententVo);
		
		return resultMap;
	}
	public Map<String, Object> updateContentSearchName(CmContentVo cmCententVo) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("message", "更新失败");
		resultMap.put("flag", "-1");
		if(cmCententVo==null||cmCententVo.getF_cc_name()==null||"".equals(cmCententVo.getF_cc_name())||contentDao==null){
			return resultMap;
		}
		Integer flag=contentDao.updateContentSearchName(cmCententVo);
		if(flag>0){
			resultMap.put("message", "更新成功");
			resultMap.put("flag", "1");
		}
		return resultMap;
	}
	public Object selectContentListByName(CmContentVo cmCententVo) {
		List<CmContentVo> list=contentDao.selectContentListByName(cmCententVo);
		return list;
	}
	public List<CmContentVo> selectContentListByCatalogId(int f_catalog_id,String f_po_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = contentDao.selectContentListByCatalogId(f_catalog_id,f_po_id,pageNo,pageSize,f_cp_cprid,contentVo);
		String adminpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.ueditor_admin_platform_path.getKey());
		String webpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.cloud_two_plane_url.getKey());
		try{
			if(adminpath!=null&&webpath!=null&&results.size()>0){
				for(int i=0;i<results.size();i++){
					CmContentVo ccVo=results.get(i);
					if(ccVo!=null){
						String desc=ccVo.getF_cc_desc();
						if(desc!=null&&!"".equals(desc)){
							desc=desc.replaceAll(adminpath, webpath);
							results.get(i).setF_cc_desc(desc);
						}
					}
				}
			}
		}catch(Exception e){
			LOG.error("内容列表详情替换失败",e);
		}
		return results;
	}
	public List<CmContentVo> selectHistoryCourse(String courseType) {
		return contentDao.selectHistoryCourse(courseType);
	}
	public List<UserHistoryVo> selectUserViewHistory(String serverId) {
		return contentDao.selectUserViewHistory(serverId);
	}
	public List<CmContentVo> selectContentGroupByCourseList(int f_catalog_id, String f_po_id, int pageNo, int pageSize,
			CmContentVo contentVo) {
		List<CmContentVo> results = contentDao.selectContentGroupByCourseList(f_catalog_id,f_po_id,pageNo,pageSize,contentVo);
		return results;
	}
	public int selectContentGroupByCourseListCount(int f_catalog_id, String f_po_id,
			CmContentVo contentVo) {
		return contentDao.selectContentGroupByCourseListCount(f_catalog_id,f_po_id,contentVo);
	}
	public int selectContentListByCourseCount(int f_catalog_id, String f_po_id, String f_cp_cprid,
			CmContentVo contentVo) {
		return contentDao.selectContentListByCourseCount(f_catalog_id,f_po_id,contentVo);
	}
}